import cloud from '@lafjs/cloud'

export default async function (ctx: FunctionContext) {
  let data = ctx.body.data
  let dbName = "moxueyuan-user-add-log"
  if (data.type == "update") dbName = "moxueyuan-user-update-log"

  let whereJson = {}
  if (data.name) whereJson["params.name"] = { $regex: data.name, $options: "i" }
  if (data.mobile) whereJson["params.mobile"] = { $regex: data.mobile, $options: "i" }

  let res = await cloud.nw.db.select({
    dbName,
    getCount: true,
    pageIndex: data.pageIndex,
    pageSize: data.pageSize,
    // 主表 where 条件
    whereJson,
    // 主表排序规则
    sortArr: [{ "name": "_add_time", "type": "desc" }],
  });

  return cloud.utils.requestReturn({ data: res })
}
